package com.tedu.ivos.dictoption.service.impl;

import com.tedu.ivos.base.exception.ServiceException;
import com.tedu.ivos.base.response.StatusCode;
import com.tedu.ivos.dict.mapper.DictMapper;
import com.tedu.ivos.dict.pojo.dto.DictQuery;
import com.tedu.ivos.dict.pojo.vo.DictVO;
import com.tedu.ivos.dictoption.mapper.DictOptionMapper;
import com.tedu.ivos.dictoption.pojo.dto.DictOptionParam;
import com.tedu.ivos.dictoption.pojo.dto.DictOptionQuery;
import com.tedu.ivos.dictoption.pojo.entity.DictOption;
import com.tedu.ivos.dictoption.pojo.vo.DictOptionVO;
import com.tedu.ivos.dictoption.service.DictOptionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class DictOptionServiceImpl implements DictOptionService {

    @Autowired
    private DictOptionMapper dictOptionMapper;

    @Autowired
    private DictMapper dictMapper;

    @Override
    public List<DictOptionVO> selectDictOption(DictOptionQuery dictOptionQuery) {
        log.debug("查询字典项信息业务，参数：{}", dictOptionQuery);
        List<DictOptionVO> list = dictOptionMapper.select(dictOptionQuery);

        return list;
    }

    @Override
    public void saveDictOption(DictOptionParam dictOptionParam) {
        DictOption dictOption = new DictOption();
        BeanUtils.copyProperties(dictOptionParam, dictOption);
        if (dictOption.getId() == null){
            log.debug("新增字典项业务，参数：{}", dictOptionParam);
            dictOption.setCreateTime(new Date());
            dictOptionMapper.insert(dictOption);
        } else {
            log.debug("编辑字典项业务，参数：{}", dictOptionParam);
            dictOption.setUpdateTime(new Date());
            dictOptionMapper.update(dictOption);
        }
    }

    @Override
    public void deleteDictOption(Long id) {
        log.debug("删除字典项业务，参数：{}", id);
        dictOptionMapper.delete(id);
    }

    @Override
    public DictOptionVO selectById(Long id) {
        log.debug("查询字典项业务，参数：{}", id);
        DictOptionVO dictOptionVO = dictOptionMapper.selectById(id);

        return dictOptionVO;
    }

    @Override
    public List<DictOptionVO> selectDictOptionByDictCode(String code) {
        log.debug("根据字典Code查字典项DictOption业务，参数：{}", code);
        // 根据现有条件Code查询字典Id
        DictQuery dictQuery = new DictQuery();
        dictQuery.setCode(code);
        List<DictVO> dictVOList = dictMapper.select(dictQuery);

        if (dictVOList != null && dictVOList.size() > 0){
            DictVO dictVO = dictVOList.get(0);
            Long dictId = dictVO.getId();
            DictOptionQuery dictOptionQuery = new DictOptionQuery();
            dictOptionQuery.setDictId(dictId);
            List<DictOptionVO> dictOptionVOList = dictOptionMapper.select(dictOptionQuery);

            return dictOptionVOList;
        } else {
            throw new ServiceException(StatusCode.DATA_UNEXISTS);
        }
    }
}
